package com.gxwy.shenzhou;

public class SelectShouXuFeiResponse implements StringHandler{
	private HeaderResponse header;
	private String serv_amt;
	public static final int SERV_AMT_LEN=15;
	public static final int SERV_AMT_FLAG=2;
	private String weizhi;
	public static final int WEIZHI_LEN=255;
	public static final int WEIZHI_FLAG=2;
	public static final int TOTAL_LEN=SERV_AMT_LEN+WEIZHI_LEN;
	private int reallen=0;
	public HeaderResponse getHeader() {
		return header;
	}

	public void setHeader(HeaderResponse header) {
		this.header = header;
	}

	public String getServ_amt() {
		return serv_amt;
	}

	public void setServ_amt(String serv_amt) throws Exception {
		if(serv_amt==null){
			Exception e=new Exception("String is null");
			throw e;
		}else if(serv_amt.getBytes("gbk").length<SERV_AMT_LEN){
			this.serv_amt=Supply.supply(serv_amt, SERV_AMT_FLAG, SERV_AMT_LEN);
		}else if(serv_amt.getBytes("gbk").length>SERV_AMT_LEN){
			 Exception e=new Exception("Too long");
			 throw e;
		}else{
			this.serv_amt = serv_amt;			
		}
	}
	
	public String getWeizhi() {
		return weizhi;
	}

	public void setWeizhi(String weizhi) throws Exception {
		if(weizhi==null){
			Exception e=new Exception("String is null");
			throw e;
		}else if(weizhi.getBytes("gbk").length<WEIZHI_LEN){
			this.weizhi=Supply.supply(weizhi, WEIZHI_FLAG, WEIZHI_LEN);
		}else if(weizhi.getBytes("gbk").length>WEIZHI_LEN){
			 Exception e=new Exception("Too long");
			 throw e;
		}else{
			this.weizhi = weizhi;			
		}
	}
	
	
	@Override
	public String toString() {
		return "SelectShouXuFeiResponse [header=" + header.toString() + ", serv_amt="
				+ serv_amt + ", weizhi=" + weizhi + ", reallen=" + reallen
				+ "]";
	}

	public String build() throws Exception{
		if(this.getHeader()==null){
			throw new Exception("Header is null");
		}
		if(this.getServ_amt()==null){
			this.setServ_amt("");
		}
		if(this.getWeizhi()==null){
			this.setWeizhi("");
		}
		Integer totallen=HeaderResponse.TOTAL_LEN+TOTAL_LEN-HeaderResponse.LEN_LEN;
		this.getHeader().setLen(totallen.toString());
		return this.getHeader().build()+this.getServ_amt()+this.getWeizhi();
	}
	
	public void parse(String str) throws Exception{
		if(str==null){
			Exception e=new Exception("String is null");
			 throw e;
		}else if(str.getBytes("gbk").length<HeaderResponse.TOTAL_LEN+TOTAL_LEN){
			Exception e=new Exception("Invalid request");
			 throw e;
		}else{
			String headerLen=SplitString.splitString(str,reallen, reallen+HeaderResponse.TOTAL_LEN);
			reallen+=HeaderResponse.TOTAL_LEN;
			HeaderResponse hr=new HeaderResponse();
			hr.parse(headerLen);
			this.setHeader(hr);
			this.setServ_amt(SplitString.splitString(str,reallen,reallen+SERV_AMT_LEN));
			reallen+=SERV_AMT_LEN;
			this.setWeizhi(SplitString.splitString(str,reallen,reallen+WEIZHI_LEN));
			reallen+=WEIZHI_LEN;
		}
	}
}
